package com.semidata.trp.decision.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;

import com.semidata.rtd.core.http.server.AbstractDispatchHandler;
import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.rtd.core.log.Logger;
import com.semidata.rtd.core.service.ServiceManager;
import com.semidata.rtd.engine.executor.TaskExecutor;
import com.semidata.rtd.engine.job.JobCompleteHandler;
import com.semidata.trp.decision.business.BusinessService;
import com.semidata.trp.decision.job.DecisionJob;
import com.semidata.trp.decision.job.DecisionJobContext;
import com.semidata.trp.decision.request.Request;
import com.semidata.trp.decision.response.Response;

public class DecisionHttpHandler extends AbstractDispatchHandler {

	public DecisionHttpHandler(ChannelHandlerContext ctx, HttpRequest request) {
		super(ctx, request);
	}

	@Override
	public void handle(final HttpSession session) {
		BusinessService businessService = ServiceManager
		        .getService(BusinessService.class);
		final DecisionJobContext context = new DecisionJobContext(session,
		        businessService.getBusinessData(),
		        businessService.getBudgetData());
		Logger.log("DEBUG", "new job start");

		TaskExecutor executor = ServiceManager.getService(TaskExecutor.class);

		DecisionJob job = DecisionJob.newDecisionJob(executor, context);

		if (job == null) {
			session.getResponse().error();
		} else {
			job.start();
		}
	}
}
